package com.hnust.controller;

import com.hnust.domain.record.ClassRecord;
import com.hnust.domain.record.CourseRecord;
import com.hnust.service.ClassService;
import com.hnust.service.TokenService;
import com.hnust.util.JsonConvert;
import com.hnust.util.Result;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 *创建人：彭鑫淼
 *创建时间：2020/10/6
 *描述：班级管理模块
 */
@CrossOrigin("*")
@RestController
@RequestMapping("/api/classes")
public class ClassController {

    @Autowired
    public ClassService classService;

    @Autowired
    public TokenService tokenService;

    /**
     * 获取所有年纪
     * @return
     */
    @GetMapping("/get/grades")
    public Result<Map> getGrades(){
        //获取所有年纪
        try{
            return new Result<Map>(1,"返回年级成功",classService.getGrades());
        }catch (Exception e){
            return new Result<Map>(0,"返回年级失败",null);
        }

    }

    /**
     * 根据年级获取所有专业
     * @param collegeId 学院Id
     * @param grade 年级
     * @return
     */
    @GetMapping("/get/majors")
    public Result<Map> getMajors(@RequestParam("collegeId") String collegeId,@RequestParam("grade") String grade){
        //获取所有的该年纪所有的专业
        try {
            return new Result<Map>(1,"返回专业成功",classService.getMajors(collegeId, grade));
        }catch (Exception e){
            return new Result<Map>(0,"返回专业失败",null);
        }
    }

    /**
     * 获取所有班级号
     * @param grade 学院Id
     * @param major 专业
     * @return
     */
    @GetMapping("/get/numbers")
    public Result<Map> getClassNumbers(@RequestParam("grade")String grade,@RequestParam("major") String major){
        //根据年纪，专业，获取所有班级号
        try {
            return new Result<Map>(1,"返回班级成功",classService.getNumbers(grade, major));
        }catch (Exception e){
            return new Result<Map>(0,"返回班级失败",null);
        }
    }

    /**
     * 获取所有班级
     * @param token token信息
     * @param collegeId 学院Id
     * @param currentPage 当前页
     * @param rows 当前页所展示的数据数量
     * @return
     */
    @GetMapping("/get")
    public Result<Map> getClasses(@RequestParam("token")String token,@RequestParam("collegeId")String collegeId,@RequestParam("currentPage") Integer currentPage,@RequestParam("rows") Integer rows){
        //验证token是否正确
        if(!tokenService.verifyToken(token, "院管理员")){
            return new Result<Map>(0,"身份验证错误",null);
        }
        //获取所有班级信息
        try {
            return new Result<Map>(1,"获取班级成功",classService.getClasses(collegeId, currentPage, rows));
        }catch (Exception e){
            return new Result<Map>(0,"获取班级失败",null);
        }
    }

    /**
     * 查询班级
     * @param map 前端返回的数据（token，collegeId，classRecord，currentPage，rows）
     * @return
     */
    @PostMapping("/query")
    public Result<Map> queryClasses(@RequestBody Map<String,Object> map){
        String token=map.get("token").toString();
        String collegeId=map.get("collegeId").toString();
        JSONObject jsonObject=JSONObject.fromObject(map.get("classRecord"));
        ClassRecord classRecord=(ClassRecord) JSONObject.toBean(jsonObject, ClassRecord.class);
        Integer currentPage=Integer.valueOf(map.get("currentPage").toString());
        Integer rows=Integer.valueOf(map.get("rows").toString());
        //验证token是否正确
        if(!tokenService.verifyToken(token, "院管理员")){
            return new Result<Map>(0,"身份验证错误",null);
        }
        //根据班级详情，查找班级
        try {
//            System.out.println("--"+ collegeId +"--");
//            System.out.println("--"+ classRecord +"--");
//            System.out.println("--"+ currentPage +"--");
//            System.out.println("--"+ rows +"--");
            return new Result<Map>(1,"查询班级成功",classService.queryClasses(collegeId, classRecord, currentPage, rows));
        }catch (Exception e){
            return new Result<Map>(0,"查询班级失败",null);
        }
    }

    /**
     * 添加班级
     * @param map 前端返回的数据（token,classRecord）
     * @return
     */
    @PostMapping("/add")
    public Result<Map> addClass(@RequestBody Map<String,Object> map){
        String token=map.get("token").toString();
        JSONObject jsonObject=JSONObject.fromObject(map.get("classRecord"));
        ClassRecord classRecord=(ClassRecord) JSONObject.toBean(jsonObject,ClassRecord.class);
       //验证token是否正确
        if(!tokenService.verifyToken(token, "院管理员")){
            return new Result<Map>(0,"身份验证错误",null);
        }
        //添加班级
        try {
            Boolean result=classService.addClasses(classRecord);
            return new Result<Map>(1,"添加班级成功",null);
        }catch (Exception e){
            return new Result<Map>(0,"添加班级失败",null);
        }
    }

    /**
     * 删除班级
     * @param token token信息
     * @param grade 年纪
     * @param majId 专业Id
     * @return
     */
    @GetMapping("/delete")
    public Result<Map> deleteSingleClass(@RequestParam("token") String token,@RequestParam("grade") String grade,@RequestParam("majId") String majId){
        //验证token是否正确
        if(!tokenService.verifyToken(token, "院管理员")){
            return new Result<Map>(0,"身份验证错误",null);
        }
        //删除班级
        try{
            Map<String,String> map=new HashMap<>();
            map.put("grade",grade);
            map.put("majId",majId);
            Boolean result=classService.deleteClass(map);
            return new Result<Map>(1,"删除班级成功",null);
        }catch (Exception e){
            return new Result<Map>(0,"删除班级失败",null);
        }
    }

    /**
     * 批量删除班级
     * @param map 前端返回的数据（token,classesIds）
     * @return
     */
    @PostMapping("/delete")
    public Result<Map> deleteBatchClass(@RequestBody Map<String,Object> map){
        String token=map.get("token").toString();
        List<Map<String,String>> list= JsonConvert.JsonToListMap(String.valueOf(map.get("records")));
        //验证token是否正确
        if(!tokenService.verifyToken(token, "院管理员")){
            return new Result<Map>(0,"身份验证错误",null);
        }
        //批量删除班级
        try{
            Boolean result=classService.deleteClass(list);
            return new Result<Map>(1,"删除课程成功",null);
        }catch (Exception e){
            return new Result<Map>(0,"删除课程失败",null);
        }
    }

    @GetMapping("/getAllClasses")
    public Result<Map> getAllClasses(@RequestParam("token")String token,@RequestParam("collegeId")String collegeId,@RequestParam("currentPage") Integer currentPage,@RequestParam("rows") Integer rows){
        //验证token是否正确
        if(!tokenService.verifyToken(token, "院管理员")){
            return new Result<Map>(0,"身份验证错误",null);
        }
        //获取所有班级信息
        try {
            return new Result<Map>(1,"获取班级成功",classService.getAllClasses(collegeId,currentPage,rows));
        }catch (Exception e){
            return new Result<Map>(0,"获取班级失败",null);
        }
    }
}
